package my.Common;

import my.app.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

public class clsUtil {
	public static Date GetDate(String date) throws Exception
	{
		SimpleDateFormat dfm;
		
		if(date.substring(0, 6).toLowerCase().equals("/date("))
		{
			return new Date(Long.valueOf(date.substring(6, 19)));
		}

		if(date.length() == 10)
		{
			dfm = new SimpleDateFormat("yyyy-MM-dd");
			date = date.replace('/', '-');
		}
		else
		{
			dfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		}		
		return dfm.parse(date);
	}
	
	public static Date GetDateFromDMY(String date) throws Exception
	{
		SimpleDateFormat dfm;
		
		dfm = new SimpleDateFormat("dd/MM/yyyy");
		
		return dfm.parse(date);
	}

	public static String FormatDate(Date date) {
		SimpleDateFormat dfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return dfm.format(date);
	}
	
	public static String FormatDateDMY(Date date) {
		SimpleDateFormat dfm = new SimpleDateFormat("dd/MM/yyyy");
		return dfm.format(date);
	}
	
	public static String FormatDateYMD(Date date) {
		SimpleDateFormat dfm = new SimpleDateFormat("yyyy-MM-dd");
		return dfm.format(date);
	}
	
	public static String FormatTime(Date date) {
		SimpleDateFormat dfm = new SimpleDateFormat("HH:mm:ss");
		return dfm.format(date);
	}
	
	public static String GetDecimalFormat(Double value)
	{		
		DecimalFormat df = new DecimalFormat("#,##0");
		return df.format(value);
	}
	
	public static Double ParseDecimalFormat(String value) throws Exception
	{		
		DecimalFormat df = new DecimalFormat("#,##0");
		return df.parse(value).doubleValue();
	}

	public static void ShowMsgBox(Context context, String msg) {
		AlertDialog.Builder ab = new AlertDialog.Builder(context);
		ab.setTitle(clsConstant.NOTICE);
		ab.setMessage(msg);		
        ab.setPositiveButton(clsConstant.CLOSE, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {		            			            	
				dialog.dismiss();
            	}
        }); 
        ab.show();
	}

	public static void ShowMsgErr(Context context, String e) {
		Intent intent = new Intent(context, frmMain.class);
    	intent.putExtra("ErrMsg", e);
		context.startActivity(intent);
	}

	
	
	public static void CopyFile(String fileNameSrc, String fileNameDest) throws IOException
	{

		InputStream in = new FileInputStream(fileNameSrc);
		OutputStream out = new FileOutputStream(fileNameDest);
		byte[] buf = new byte[1024];
		int len;
		while ((len = in.read(buf)) > 0) {
		   out.write(buf, 0, len);
		}
		in.close();
		out.close(); 	
	}
	
	public static Double Round(Double value, int decimalPlace)
	{
		Double dblResult = value*Math.pow(10, decimalPlace);
		dblResult = (double) Math.round(dblResult);
		return dblResult/Math.pow(10, decimalPlace);
	}
}